{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "from miasm.analysis.machine import Machine\n",
    "from miasm.arch.x86.arch import mn_x86\n",
    "from miasm.core import parse_asm\n",
    "from miasm.core.locationdb import LocationDB\n",
    "from future.utils import viewitems\n",
    "from miasm.analysis.data_flow import *\n",
    "import pydotplus\n",
    "from IPython.display import Image, display_png"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "loc_db = LocationDB()\n",
    "asmcfg = parse_asm.parse_txt(mn_x86, 32, ''' \n",
    "main:\n",
    "    PUSH EBP\n",
    "    MOV EBP, ESP\n",
    "    MOV ECX, 0x23\n",
    "    MOV ECX, 0x4\n",
    "    MOV EAX, ECX\n",
    "    POP EBP\n",
    "    RET\n",
    "''', loc_db)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "loc_db.set_location_offset(loc_db.get_name_location(\"main\"), 0x0)\n",
    "\n",
    "machine = Machine('x86_32')\n",
    "ir_arch = machine.ira(loc_db)\n",
    "ircfg = ir_arch.new_ircfg_from_asmcfg(asmcfg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Before Simplification:')\n",
    "for lbl, irb in viewitems(ircfg.blocks):\n",
    "    print(irb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "deadrm = DeadRemoval(ir_arch)\n",
    "deadrm(ircfg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('After Simplification:')\n",
    "for lbl, irb in viewitems(ircfg.blocks):\n",
    "    print(irb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
